package com.qiyun.dinghuohui;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiyun.dinghuohui.base.BaseActivity;
import com.qiyun.dinghuohui.base.FragmentManager;
import com.qiyun.dinghuohui.common.ApiName;
import com.qiyun.dinghuohui.common.CommonDataSets;
import com.qiyun.dinghuohui.dialog.TipDialog;
import com.qiyun.dinghuohui.fragment.AnalyseOrderFragment;
import com.qiyun.dinghuohui.fragment.DisplayOrderFragment;
import com.qiyun.dinghuohui.fragment.FreeOrderFragment;
import com.qiyun.dinghuohui.fragment.MoreFragment;
import com.qiyun.dinghuohui.fragment.TogetherOrderFragment;
import com.qiyun.dinghuohui.utils.AsyncJsonTask;
import com.qiyun.dinghuohui.utils.Constants;
import com.qiyun.dinghuohui.utils.Inject;
import com.qiyun.dinghuohui.utils.JsonBuilder;
import com.qiyun.dinghuohui.utils.LinearGradientUtil;
import com.qiyun.dinghuohui.utils.LogDebug;
import com.qiyun.dinghuohui.utils.ServerResponse;
import com.qiyun.dinghuohui.vo.PublicSelectParamBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import qiu.niorgai.StatusBarCompat;

public class MainActivity extends BaseActivity {


    @SuppressLint("StaticFieldLeak")
    public static MainActivity instance;
    private Handler mHandler;
    public static ApplicationDataHolder adh;
    private static final int OP_LOGOUT = 886;

    private FragmentManager mFragmentManager;
    @Inject(R.id.user_name)
    private TextView mTxtName;//账号名称
    @Inject(R.id.order_price)
    private TextView mTxtMoney;//订单件数和金额
    @Inject(R.id.drawer_layout)
    private DrawerLayout mDrawerLayout;
    @Inject(R.id.menu_layout_left)
    private RelativeLayout mRelaDrawer;

    @Inject(R.id.select_recycle0)
    private RecyclerView mSelectRecycle0;

    @Inject(R.id.select_recycle1)
    private RecyclerView mSelectRecycle1;

    @Inject(R.id.select_recycle2)
    private RecyclerView mSelectRecycle2;

    @Inject(R.id.select_recycle3)
    private RecyclerView mSelectRecycle3;

    @Inject(R.id.select_recycle4)
    private RecyclerView mSelectRecycle4;

    @Inject(R.id.select_recycle5)
    private RecyclerView mSelectRecycle5;

    @Inject(R.id.select_recycle6)
    private RecyclerView mSelectRecycle6;

    @Inject(R.id.select_recycle7)
    private RecyclerView mSelectRecycle7;
    @Inject(R.id.slide_scroll)
    private NestedScrollView mScroll;
    @Inject(R.id.free_sx)
    private LinearLayout mLLFreeSx;
    @Inject(R.id.together_sx)
    private LinearLayout mLLTogetherSx;

    private TextView mSxConfirm;//筛选确认

    private TextView mSxCancle;//筛选取消
    private RelativeLayout rl;
    private boolean isStyleAll = true;
    private String s;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initViews() {
        adh = (ApplicationDataHolder) getApplication();
        mTxtName.setText(String.valueOf(adh.getBoardInfo_name()));
        String cout = CommonDataSets.getOrderCount();
        String money = CommonDataSets.getOrdersMoney();
        String allMoney = cout + "件" + "   " + money + "元";
        mTxtMoney.setText(String.valueOf(allMoney));
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        rl = (RelativeLayout) findViewById(R.id.menu_layout_left);
        mSxConfirm = (TextView) rl.findViewById(R.id.sx_confirm);
        mSxCancle = (TextView) rl.findViewById(R.id.sx_cancle);
    }

    private Fragment context;

    /**
     * 切换DrawerLayout
     */
    public void tooggleDrawer(Fragment context) {
        this.context = context;
        if (mDrawerLayout.isDrawerOpen(mRelaDrawer)) {
            mDrawerLayout.closeDrawer(mRelaDrawer);
        } else {
            mDrawerLayout.openDrawer(mRelaDrawer);
        }
        if (context instanceof TogetherOrderFragment) {
            //隐藏组别筛选外的其他筛选。
            mLLFreeSx.setVisibility(View.GONE);
            mLLTogetherSx.setVisibility(View.VISIBLE);

        } else if (context instanceof FreeOrderFragment) {
            mLLFreeSx.setVisibility(View.VISIBLE);
            mLLTogetherSx.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.free_order:
                showFreeOrderFragment();
                break;
            case R.id.together_order:
                showTogetherOrderFragment();
                break;
            case R.id.display_order:
                showDisplayOrderFragment();
                break;
            case R.id.analyse_order:
                showAnalyseOrderFragment();
                break;
            case R.id.more:
                showMoreFragment();
                break;
            case R.id.sx_confirm:
                isConfirmClick = true;
                tooggleDrawer(context);
                break;
            case R.id.sx_cancle:
                isConfirmClick = true;
                restFilter();
                break;
        }
    }

    //重置搜索条件
    private void restFilter() {
        filter.clear();
        groupAdapter.setNewData(getGroupData());
        pinPAdapter.setNewData(getPinpData());
        jiJAdapter.setNewData(getJiJData());
        xiLAdapter.setNewData(getXiLData());
        boDAdapter.setNewData(getBoDData());
        daLAdapter.setNewData(getDaLData());
        xiaoLAdapter.setNewData(getXiaoLData());
        kuanSAdapter.setNewData(getKuanSData());
    }

    private GroupAdapter groupAdapter;
    private PinPAdapter pinPAdapter;
    private JiJAdapter jiJAdapter;
    private XiLAdapter xiLAdapter;
    private BoDAdapter boDAdapter;
    private DaLAdapter daLAdapter;
    private XiaoLAdapter xiaoLAdapter;
    private KuanSAdapter kuanSAdapter;
    String[] keys = new String[]{"brand", "season", "serial", "wave", "type", "style", "v", "pairgroup"};
    Map<String, JSONObject> filter = new HashMap<>();
    private String filter_together;
    //    //组别搜索
//            if (isTogether!=null) {
//        List<JSONObject> list = new ArrayList<>();
//        for (JSONObject o : ds_pair.values()) {
//
//        }
//        return list;
//    }
    private FreeOrderFragment freeOrderFragment;
    private TogetherOrderFragment togetherOrderFragment;
    private DisplayOrderFragment displayOrderFragment;
    private AnalyseOrderFragment analyseOrderFragment;
    private MoreFragment moreFragment;
    private boolean isConfirmClick = false;

    @Override
    protected void onSetListener() {

        mllFreeOrder.setOnClickListener(this);
        mllTogetherOrder.setOnClickListener(this);
        mllDisplayOrder.setOnClickListener(this);
        mllAnalysOrder.setOnClickListener(this);
        mllMoreOrder.setOnClickListener(this);
        mSxConfirm.setOnClickListener(this);
        mSxCancle.setOnClickListener(this);

        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                //两种颜色之间的线性变化
                if (0.0 <= slideOffset && slideOffset <= 1.0) {
                    StatusBarCompat.setStatusBarColor(getThis(), new LinearGradientUtil(Color.parseColor("#FF7170"),
                            Color.parseColor("#652D2C")).getColor(slideOffset));
                }
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                isConfirmClick = false;
                mScroll.scrollTo(0, 0);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                if (isConfirmClick) {
                    if (context instanceof FreeOrderFragment) {
                        freeOrderFragment.filterOptions(filter);
                    } else if (context instanceof TogetherOrderFragment) {
                        togetherOrderFragment.filterOptions(filter_together);
                    } else if (context instanceof DisplayOrderFragment) {

                    } else if (context instanceof AnalyseOrderFragment) {

                    } else if (context instanceof MoreFragment) {

                    }
                }
            }
        });
        groupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = getGroupData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                groupAdapter.setNewData(list);
            }
        });
        pinPAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = getPinpData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[0], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                pinPAdapter.setNewData(list);
            }
        });
        jiJAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = getJiJData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[1], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                jiJAdapter.setNewData(list);
            }
        });
        xiLAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = getXiLData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[2], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                xiLAdapter.setNewData(list);
            }
        });
        boDAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = getBoDData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[3], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                boDAdapter.setNewData(list);
            }
        });
        daLAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                List<JSONObject> list = getDaLData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[4], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                daLAdapter.setNewData(list);

                if (position != 0) {
                    s = list.get(position).optString("s");
                    List<JSONObject> list1 = getStyleDataByType(s);
                    filter.put(keys[5], list.get(0));
                    xiaoLAdapter.setNewData(list1);
                    isStyleAll = false;
                } else {
                    xiaoLAdapter.setNewData(getXiaoLData());
                    filter.put(keys[5], list.get(0));
                    isStyleAll = true;
                }
            }
        });

        xiaoLAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = null;
                if (isStyleAll) {
                    list = getXiaoLData();
                } else {
                    list = (List<JSONObject>) adapter.getData();
                }
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[5], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                xiaoLAdapter.setNewData(list);

            }
        });
        kuanSAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<JSONObject> list = getKuanSData();
                for (int i = 0; i < list.size(); i++) {
                    try {
                        if (i == position) {
                            filter.put(keys[6], list.get(position));
                            list.get(i).put("select", true);
                        } else {
                            list.get(i).put("select", false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                kuanSAdapter.setNewData(list);
            }
        });
    }

    //通过type中的s获取style
    private List<JSONObject> getStyleDataByType(String s) {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[5]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (JSONObject o : getXiaoLData()) {
            String type = o.optString("type");
            if (type.equals(s)) {
                try {
                    o.put("select", false);
                    o.put("name", o.optString("style"));
                    o.put("flag", keys[5]);
                    list.add(o);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    private List<JSONObject> getGroupData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[7]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (JSONObject o : CommonDataSets.ds_pair.values()) {
            try {
                String value = o.optString("pairgroup");
                if (!"null".equals(value)) {
                    o.put("select", false);
                    o.put("name", value);
                    o.put("flag", keys[7]);
                    list.add(o);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getPinpData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[0]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (JSONObject o : CommonDataSets.ds_brand) {
            try {
                o.put("select", false);
                o.put("name", o.optString("brand"));
                o.put("flag", keys[0]);
                list.add(o);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getJiJData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[1]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (Object g : CommonDataSets.ds_seasons.entrySet()) {
            Map.Entry entry = (Map.Entry) g;
            JSONObject pair = (JSONObject) entry.getValue();
            try {
                pair.put("select", false);
                pair.put("name", pair.optString("season"));
                pair.put("flag", keys[1]);
                list.add(pair);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getXiLData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[2]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (Object g : CommonDataSets.ds_serials.entrySet()) {
            Map.Entry entry = (Map.Entry) g;
            JSONObject pair = (JSONObject) entry.getValue();
            try {
                pair.put("select", false);
                pair.put("name", pair.optString("serial"));
                pair.put("flag", keys[2]);
                list.add(pair);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getBoDData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[3]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (JSONObject o : CommonDataSets.ds_wave) {
            try {
                o.put("select", false);
                o.put("name", o.optString("wave"));
                o.put("flag", keys[3]);
                list.add(o);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getDaLData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[4]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (Object g : CommonDataSets.ds_type.entrySet()) {
            Map.Entry entry = (Map.Entry) g;
            JSONObject pair = (JSONObject) entry.getValue();
            try {
                pair.put("select", false);
                pair.put("name", pair.optString("type"));
                pair.put("flag", keys[4]);
                list.add(pair);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getXiaoLData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[5]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (Object g : CommonDataSets.ds_style.entrySet()) {
            Map.Entry entry = (Map.Entry) g;
            JSONObject pair = (JSONObject) entry.getValue();
            try {
                pair.put("select", false);
                pair.put("name", pair.optString("style"));
                pair.put("flag", keys[5]);
                list.add(pair);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private List<JSONObject> getKuanSData() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        try {
            object.put("name", "全部");
            object.put("select", true);
            object.put("flag", keys[6]);
            list.add(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (Object g : CommonDataSets.ds_goodslevel.entrySet()) {
            Map.Entry entry = (Map.Entry) g;
            JSONObject pair = (JSONObject) entry.getValue();
            try {
                pair.put("select", false);
                pair.put("name", pair.optString("v"));
                pair.put("flag", keys[6]);
                list.add(pair);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        initHandler();
        instance = this;
        mFragmentManager = initFragmentManager(R.id.framelayout);
        showFreeOrderFragment();
        setAdapter();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                exissystem();
                return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Inject(R.id.free_order)
    private LinearLayout mllFreeOrder;//自由订货
    @Inject(R.id.together_order)
    private LinearLayout mllTogetherOrder; //搭配订货
    @Inject(R.id.display_order)
    private LinearLayout mllDisplayOrder; //陈列订货
    @Inject(R.id.analyse_order)
    private LinearLayout mllAnalysOrder;//订单分析
    @Inject(R.id.more)
    private LinearLayout mllMoreOrder;//更多


    //自由订货
    private void showFreeOrderFragment() {
        mllFreeOrder.setSelected(true);
        mllTogetherOrder.setSelected(false);
        mllDisplayOrder.setSelected(false);
        mllAnalysOrder.setSelected(false);
        mllMoreOrder.setSelected(false);
        if (freeOrderFragment == null) {
            freeOrderFragment = new FreeOrderFragment();
        }
        mFragmentManager.showFragment(freeOrderFragment);
    }


    //搭配订货
    private void showTogetherOrderFragment() {
        mllTogetherOrder.setSelected(true);
        mllFreeOrder.setSelected(false);
        mllDisplayOrder.setSelected(false);
        mllAnalysOrder.setSelected(false);
        mllMoreOrder.setSelected(false);
        if (togetherOrderFragment == null) {
            togetherOrderFragment = new TogetherOrderFragment();
        }
        mFragmentManager.showFragment(togetherOrderFragment);
    }


    //陈列订货
    private void showDisplayOrderFragment() {
        mllDisplayOrder.setSelected(true);
        mllFreeOrder.setSelected(false);
        mllTogetherOrder.setSelected(false);
        mllAnalysOrder.setSelected(false);
        mllMoreOrder.setSelected(false);
        if (displayOrderFragment == null) {
            displayOrderFragment = new DisplayOrderFragment();
        }
        mFragmentManager.showFragment(displayOrderFragment);
    }


    //订单分析
    private void showAnalyseOrderFragment() {
        mllAnalysOrder.setSelected(true);
        mllDisplayOrder.setSelected(false);
        mllFreeOrder.setSelected(false);
        mllTogetherOrder.setSelected(false);
        mllMoreOrder.setSelected(false);
        if (analyseOrderFragment == null) {
            analyseOrderFragment = new AnalyseOrderFragment();
        }
        mFragmentManager.showFragment(analyseOrderFragment);
    }


    //更多
    private void showMoreFragment() {
        mllMoreOrder.setSelected(true);
        mllAnalysOrder.setSelected(false);
        mllDisplayOrder.setSelected(false);
        mllFreeOrder.setSelected(false);
        mllTogetherOrder.setSelected(false);
        if (moreFragment == null) {
            moreFragment = new MoreFragment();
        }
        mFragmentManager.showFragment(moreFragment);
    }

    @Override
    public void onPostDataStart() {

    }

    @Override
    public void onPostDataSuccess(ServerResponse response) {

    }

    @Override
    public void onPostDataError(ServerResponse response) {

    }

    @Override
    public void onPostDataComplete(ServerResponse response) {

    }

    @Override
    public void setProgressMessage(String message) {

    }

    @Override
    public void publicSelectCallBack(PublicSelectParamBean pspb) {

    }

    private void initHandler() {
        mHandler = new Handler(getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case Constants.SHOW_LOADING:
                        AppDialogShow();
                        break;
                    case Constants.HIDE_LOADING:
                        AppDialogHide();
                        break;
                    // case Constants.UPDATE_REPORT:
                    // cv_commit_wv_html.loadDataWithBaseURL("",calcWcl(0,cv_pub_head_commit_btn),
                    // "text/html", "UTF-8","");
                    // break;
                    case 9999:
//					String board_info = String.format(
//							getResources().getString(R.string.board_info_2),
//							 adh.getBoardInfo_budget(),star_price_dpe+"元 数量"+star_allcount+"件 款量"+star_stylecount);
//					board.setText(board_info);
//                        changeText(tv_money02, str2, CommonDataSets.getDoubleForString(star_price_dpe), " 元", false);
//                        changeText(tv_zongliang02, "数量：", CommonDataSets.getDoubleForString(star_allcount), " 件", false);
//                        changeText(tv_kuanlang02, "款量：", CommonDataSets.getDoubleForString(star_stylecount), " 款", false);
//                        if (adh.getLevel() == null)
//                        {
//                            adh.setLevel("");
//                        }
//                        if (adh.getUserType() == 0)
//                        {
//                        }
//                        else if (adh.getUserType() == 1)
//                        { // 欢迎参加%1$s货品订货会,您的计划为%2$s.祝您圆满完成订货计划!
//						board_info = "尊敬的【" + adh.getBoardInfo_name() + "】，您控制的组别是【"
//								+ adh.getBoardInfo_groupname() + "】，祝您圆满完成订货！";
//						board.setText(board_info);
//                        }
                        break;
                    default:
                        break;
                }
                super.handleMessage(msg);
            }
        };
    }

    private static final int OP_SYNC_DATA = 9999;
    private static final int OP_SYNC_UPDATA = 8888;
    private static final int BATTERY = 0;

    private void QuitSystemOpt() {
        adh.getWifiAdminOpen();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    private void exissystem() {
        // 判断有无需要同步的数据
        if (checkDataNeedSync(this, adh, mHandler, OP_SYNC_DATA, false, true)) {
            return;
        }
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("您是要退出订货系统吗？")
                .setMessage("")
                .setPositiveButton("退出订货",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // 如果启用了客户端验证，则调用退出操作
                                if (CommonDataSets.ds_config_activity.clientcheck) {
                                    mHandler.sendEmptyMessage(Constants.SHOW_LOADING);
                                    JSONObject postData = null;
                                    try {
                                        JsonBuilder jb = new JsonBuilder();
                                        jb.setUt(0);
                                        postData = jb
                                                .setUsername(adh.getUsername())
                                                .setPassword("")
                                                .setDeviceId(adh.getDeviceId())
                                                .setBattery(adh.getBattery())
                                                .setDeviceId(adh.getWifiMacAddress())
                                                .setOp(OP_LOGOUT).buildJson();
                                        // new
                                        // AsyncJsonTask("/basic/config!login.action",
                                        // this).execute(postData);
                                        new AsyncJsonTask(
                                                adh.getServerUrl(true, 0, "B"),
                                                ApiName.LOGIN_OUT,
                                                MainActivity.this)
                                                .execute(postData);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        mHandler.sendEmptyMessage(Constants.HIDE_LOADING);
                                    }

                                } else {
                                    QuitSystemOpt();
                                }
                            }
                        })
                .setNegativeButton("继续订货",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {

                            }
                        }).show();
    }


    private void setAdapter() {
        setGroupAdapter();
        setPinPAdapter();
        setJiJAdapter();
        setXiLAdapter();
        setBoDAdapter();
        setDaLAdapter();
        setXiaoLAdapter();
        setKuanSAdapter();
    }

    private void setGroupAdapter() {
        groupAdapter = new GroupAdapter(R.layout.item_select_content, getGroupData());
        mSelectRecycle0.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle0.setHasFixedSize(true);
        mSelectRecycle0.setAdapter(groupAdapter);
    }

    private void setPinPAdapter() {
        pinPAdapter = new PinPAdapter(R.layout.item_select_content, getPinpData());
        mSelectRecycle1.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle1.setHasFixedSize(true);
        mSelectRecycle1.setAdapter(pinPAdapter);
    }

    private void setJiJAdapter() {
        jiJAdapter = new JiJAdapter(R.layout.item_select_content, getJiJData());
        mSelectRecycle2.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle2.setHasFixedSize(true);
        mSelectRecycle2.setAdapter(jiJAdapter);
    }

    private void setXiLAdapter() {
        xiLAdapter = new XiLAdapter(R.layout.item_select_content, getXiLData());
        mSelectRecycle3.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle3.setHasFixedSize(true);
        mSelectRecycle3.setAdapter(xiLAdapter);
    }

    private void setBoDAdapter() {
        boDAdapter = new BoDAdapter(R.layout.item_select_content, getBoDData());
        mSelectRecycle4.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle4.setHasFixedSize(true);
        mSelectRecycle4.setAdapter(boDAdapter);
    }

    private void setDaLAdapter() {
        daLAdapter = new DaLAdapter(R.layout.item_select_content, getDaLData());
        mSelectRecycle5.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle5.setHasFixedSize(true);
        mSelectRecycle5.setAdapter(daLAdapter);
    }

    private void setXiaoLAdapter() {
        xiaoLAdapter = new XiaoLAdapter(R.layout.item_select_content, getXiaoLData());
        mSelectRecycle6.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle6.setHasFixedSize(true);
        mSelectRecycle6.setAdapter(xiaoLAdapter);
    }

    private void setKuanSAdapter() {
        kuanSAdapter = new KuanSAdapter(R.layout.item_select_content, getKuanSData());
        mSelectRecycle7.setLayoutManager(new GridLayoutManager(getThis(), 3, LinearLayoutManager.VERTICAL, false));
        mSelectRecycle7.setHasFixedSize(true);
        mSelectRecycle7.setAdapter(kuanSAdapter);
    }

    //组别
    public class GroupAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public GroupAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
                filter_together = item.optString("name");
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //品牌
    public class PinPAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public PinPAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //季节
    public class JiJAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public JiJAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //系列
    public class XiLAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public XiLAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //波段
    public class BoDAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public BoDAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //大类
    public class DaLAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public DaLAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //小类
    public class XiaoLAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public XiaoLAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

    //款式定位
    public class KuanSAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public KuanSAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            boolean isSelect = item.optBoolean("select");
            ImageView imageView = helper.getView(R.id.select_imag);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.select_txt);
            lp.addRule(RelativeLayout.ALIGN_RIGHT, R.id.select_txt);
            if (isSelect) {
                lp.setMargins(0, 0, 0, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(true);
            } else {
                lp.setMargins(0, 0, 1, 1);
                imageView.setLayoutParams(lp);
                helper.itemView.setSelected(false);
            }
            helper.setText(R.id.select_txt, item.optString("name"));
        }
    }

}
